{% extends 'base.html' %}
{% load paper_filters %}

{% block title %}Search Results - Research Paper Analyzer{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <!-- Header with Export Options -->
        <div class="card mb-4">
            <div class="card-header">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h3 class="mb-0">
                            <i class="fas fa-search me-2"></i>Search Results
                        </h3>
                        <p class="text-muted mb-0">
                            Query: <strong>"{{ query }}"</strong> | 
                            Papers found: <strong>{{ papers_count }}</strong>
                        </p>
                    </div>
                    <div class="btn-group">
                        <a href="{% url 'papers:export_csv' %}" class="btn btn-success">
                            <i class="fas fa-file-csv me-1"></i>Download CSV
                        </a>
                        <a href="{% url 'papers:search' %}" class="btn btn-outline-primary">
                            <i class="fas fa-search me-1"></i>New Search
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <!-- Analysis Summary -->
        {% if analysis_summary %}
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-chart-bar me-2"></i>Analysis Summary
                </h5>
            </div>
            <div class="card-body">
                <p class="lead">{{ analysis_summary }}</p>
            </div>
        </div>
        {% endif %}

        <!-- Papers List -->
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-file-alt me-2"></i>Analyzed Papers ({{ papers_count }})
                </h5>
            </div>
            <div class="card-body">
                {% if papers %}
                    <div class="row">
                        {% for paper in papers %}
                            <div class="col-12 mb-4">
                                <div class="card border">
                                    <div class="card-header bg-light">
                                        <h6 class="mb-0 text-primary">{{ paper.title }}</h6>
                                    </div>
                                    <div class="card-body">
                                        <div class="row mb-3">
                                            <div class="col-md-6">
                                                <small class="text-muted">
                                                    <i class="fas fa-users me-1"></i>
                                                    <strong>Authors:</strong> {{ paper.authors }}
                                                </small>
                                            </div>
                                            <div class="col-md-3">
                                                <small class="text-muted">
                                                    <i class="fas fa-calendar me-1"></i>
                                                    <strong>Year:</strong> {{ paper.year|default:"N/A" }}
                                                </small>
                                            </div>
                                            <div class="col-md-3">
                                                <small class="text-muted">
                                                    <i class="fas fa-database me-1"></i>
                                                    <strong>Source:</strong> {{ paper.source|default:"Unknown" }}
                                                </small>
                                            </div>
                                        </div>
                                        
                                        <!-- Full Abstract -->
                                        <div class="mb-3">
                                            <h6><i class="fas fa-file-text text-info me-1"></i>Abstract</h6>
                                            <p class="card-text">{{ paper.abstract }}</p>
                                        </div>
                                        
                                        <!-- Extracted Information -->
                                        {% if paper.main_findings %}
                                        <div class="mb-3">
                                            <h6><i class="fas fa-lightbulb text-success me-1"></i>Main Findings</h6>
                                            <p class="card-text">{{ paper.main_findings }}</p>
                                        </div>
                                        {% endif %}
                                        
                                        {% if paper.methodology %}
                                        <div class="mb-3">
                                            <h6><i class="fas fa-microscope text-info me-1"></i>Methodology</h6>
                                            <p class="card-text">{{ paper.methodology }}</p>
                                        </div>
                                        {% endif %}
                                        
                                        {% if paper.interventions %}
                                        <div class="mb-3">
                                            <h6><i class="fas fa-tools text-warning me-1"></i>Interventions/Approaches</h6>
                                            <p class="card-text">{{ paper.interventions }}</p>
                                        </div>
                                        {% endif %}
                                        
                                        {% if paper.outcomes %}
                                        <div class="mb-3">
                                            <h6><i class="fas fa-chart-line text-success me-1"></i>Outcomes/Results</h6>
                                            <p class="card-text">{{ paper.outcomes }}</p>
                                        </div>
                                        {% endif %}
                                        
                                        {% if paper.statistical_data %}
                                        <div class="mb-3">
                                            <h6><i class="fas fa-chart-pie text-primary me-1"></i>Statistical Data</h6>
                                            <p class="card-text">{{ paper.statistical_data }}</p>
                                        </div>
                                        {% endif %}
                                        
                                        {% if paper.limitations %}
                                        <div class="mb-3">
                                            <h6><i class="fas fa-exclamation-triangle text-danger me-1"></i>Limitations</h6>
                                            <p class="card-text">{{ paper.limitations }}</p>
                                        </div>
                                        {% endif %}
                                        
                                        {% if paper.research_gaps %}
                                        <div class="mb-3">
                                            <h6><i class="fas fa-question-circle text-warning me-1"></i>Research Gaps</h6>
                                            <p class="card-text">{{ paper.research_gaps }}</p>
                                        </div>
                                        {% endif %}
                                        
                                        {% if paper.future_research %}
                                        <div class="mb-3">
                                            <h6><i class="fas fa-rocket text-info me-1"></i>Future Research</h6>
                                            <p class="card-text">{{ paper.future_research }}</p>
                                        </div>
                                        {% endif %}
                                        
                                        <div class="mt-3">
                                            {% if paper.doi %}
                                            <span class="badge bg-info me-2">DOI: {{ paper.doi }}</span>
                                            {% endif %}
                                            <span class="badge bg-secondary">{{ paper.source|default:"Unknown" }}</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        {% endfor %}
                    </div>
                {% else %}
                    <div class="text-center py-5">
                        <i class="fas fa-search fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">No papers found</h5>
                        <p class="text-muted">Try adjusting your search query or parameters.</p>
                        <a href="{% url 'papers:search' %}" class="btn btn-primary">
                            <i class="fas fa-search me-1"></i>Try New Search
                        </a>
                    </div>
                {% endif %}
            </div>
        </div>

        <!-- Export Section -->
        <div class="card mt-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-download me-2"></i>Export Results
                </h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-8">
                        <h6>CSV Export</h6>
                        <p class="small text-muted">Download all detailed analysis data in CSV format for further analysis in Excel or other spreadsheet software.</p>
                        <a href="{% url 'papers:export_csv' %}" class="btn btn-success btn-lg">
                            <i class="fas fa-file-csv me-1"></i>Download CSV File
                        </a>
                    </div>
                    <div class="col-md-4">
                        <div class="text-center">
                            <div class="display-4 text-success">{{ papers_count }}</div>
                            <p class="text-muted">Papers Ready for Export</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
$(document).ready(function() {
    // Add click handlers for paper cards
    $('.card').hover(
        function() {
            $(this).addClass('shadow-sm');
        },
        function() {
            $(this).removeClass('shadow-sm');
        }
    );
    
    // Initialize tooltips
    $('[data-bs-toggle="tooltip"]').tooltip();
});
</script>
{% endblock %} 